//
//  TCPClient.h
//  LazyHome
//
//  Created by yh on 2018/3/30.
//  Copyright © 2018年 yh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CocoaAsyncSocket/GCDAsyncSocket.h>
#import <AFNetworking/AFNetworkReachabilityManager.h>
#import <AFNetworking/AFNetworkActivityIndicatorManager.h>


typedef enum : NSUInteger {
    TCPClientStatusInit,
    TCPClientStatusOnConnecting,
    TCPClientStatusConnected,
    TCPClientStatusConnectException, //连接异常
    TCPClientStatusDisConnect,//正常不在连接状态
    TCPClientStatusOnReconnecting,
} TCPClientStatus;


@class TCPClient;
@protocol TCPClientDelegate<NSObject>

/**
 网络切换后是否允许重新连接

 @param tcpClient tcpClient description
 @param networkReachabilityStatus tcp
 @return  s
 */
- (BOOL)tcpClient:(TCPClient *)tcpClient shouldReconnectForNetChange:(AFNetworkReachabilityStatus)networkReachabilityStatus;

- (void)tcpClient:(TCPClient *)tcpClient socket:(GCDAsyncSocket *)sock didWriteData:(NSData *)data withTag:(long)tag;

- (void)tcpClient:(TCPClient *)tcpClient socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;

- (void)tcpClient:(TCPClient *)tcpClient writeChannelFreeInTimer:(NSTimer *)timer;

- (void)tcpClient:(TCPClient *)tcpClient readChannelFreeInTimer:(NSTimer *)timer;

- (void)tcpClient:(TCPClient *)tcpClient connectStatusChange:(TCPClientStatus)status;


@end



@interface TCPClient : NSObject<GCDAsyncSocketDelegate>
@property (nonatomic, copy) NSString *localport;
@property (nonatomic, copy) NSString *host;
@property (nonatomic, copy) NSString *port;
@property (nonatomic, strong) GCDAsyncSocket *tcpSocket;
@property (nonatomic, assign) id <TCPClientDelegate> delegate;

@property (nonatomic, strong) NSMutableDictionary *sendtagData;
@property (nonatomic, strong) NSMutableData *sendBuffer;
@property (nonatomic, strong) NSMutableData *reciveBuffer;
@property (nonatomic, assign) TCPClientStatus tcpClientStatus;

+ (instancetype)clientWithHost:(NSString *)host
                          port:(NSString *)port
                     localport:(NSString *)localport
                         error:(NSError **)errPtr;

- (instancetype)initWithHost:(NSString *)host
                        port:(NSString *)port
                   localport:(NSString *)localport
                       error:(NSError **)errPtr;

- (void)connectWithError:(NSError **)errPtr;

- (void)sendData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;

//用户主动结束连接
- (void)normal_disConnectByUser;

/**
 长时间没收到服务器发过来的数据,进行处理
 */
- (void)resetConnect;

@end
